<?php

defined('BY_JYA') or exit('error');
/*
	游戏营销
*/
$tabs=array(
	array('op'=>'list','title'=>'活动列表'),
	array('op'=>'edit','title'=>'添加/编辑游戏活动'),
);
set_html('tabs',$tabs);
$_RQ['op']=$_RQ['op']?:'list';
$return['types']=$this->canyu_types;
$return['rewardtype']=$this->rewardtype;
$return['get_types']=$this->get_types;
$return['times']=$this->times;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_red_pin');	
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['title']){
		$params['title like']=trim($_RQ['title']);
		$return['params']['title']=$_RQ['title'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array(intval($_RQ['page'])?:1,10),
	);
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_weishop_game',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['pluginset']=json_decode($item['pluginset'],true);
		$item['url']=custom_url('jy_red_pin/game//id='.$item['id'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'活动标题','type'=>'string'),
			array('name'=>'type','title'=>'参与条件','type'=>'radio','options'=>'types'),
			array('name'=>'time','title'=>'弹窗时机','type'=>'radio','options'=>'times'),
			array('name'=>'pluginset.rewardtype','title'=>'奖励类型','type'=>'radio','options'=>'rewardtype'),
			array('name'=>'status','title'=>'活动状态','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'活动链接','type'=>'copy'),
			array('name'=>'url','title'=>'活动码','type'=>'ewm'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'活动状态','options'=>'statuss'),
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'活动标题'),
		),
	);	
	$return['htmls']=$htmls;
	$return['statuss']=array(
		'0'=>'关闭',
		'1'=>'启用',
	);
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	$post=get_postdata();
	$status=intval($post['status']);
	pdo_update('jy_weishop_game',array('status'=>$status),array('id'=>$id));
	if($status==0){
		exi('关闭成功！');
	}else{
		exi('开启成功！');
	}
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if($post['pluginset']['red_max']<$post['pluginset']['red_min']){
			exi('奖励上限不能小于奖励下限！','error');
		}
		$post['pluginset']['red_shareset']=$post['red_shareset'];
		$data=array(
			'title'=>trim($post['title']),
			'game_id'=>intval($post['game_id']),
			'type'=>intval($post['type']),
			'price'=>round($post['price'],2),
			'goodsid'=>intval($post['goodsid']),
			'time'=>intval($post['time']),
			'status'=>intval($post['status']),
			'sendtitle'=>trim($post['sendtitle']),
			'sendimage'=>trim($post['sendimage']),
			'senddesc'=>trim($post['senddesc']),
			'pluginset'=>ijson_encode($post['pluginset']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('jy_weishop_game',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$data['plugin_name']='jy_red_pin';
			pdo_insert('jy_weishop_game',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_game',array('id'=>$id));
		$item['pluginset']=$item['pluginset']?json_decode($item['pluginset'],true):array('rewardtype'=>0);
		$item['red_shareset']=$item['pluginset']['red_shareset']?:array();
	}else{
		$item=array('title'=>'','content'=>array('goodsid'=>''));
		$item['pluginset']=array();
		$item['red_shareset']=array();
	}
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个参与奖励活动机会';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../addons/jy_red_pin/images/red_rain.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'活动有时间限制，请及时参与！';

	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('name'=>'title','title'=>'活动标题','type'=>'string'),
			array('type'=>'radio','title'=>'奖励类型','name'=>'pluginset.rewardtype','options'=>'rewardtype','help'=>'会员红包:是指系统会员红包，金额大小不受限制；现金红包：是只微信零钱，抽中后，直接发送微信红包给中奖者'),
			array('type'=>'unitstring','unit'=>'个','title'=>'奖励上限','name'=>'pluginset.red_max'),
			array('type'=>'unitstring','unit'=>'个','title'=>'奖励下限','name'=>'pluginset.red_min'),
			array('type'=>'radio','title'=>'领取奖励类型','name'=>'pluginset.get_type','options'=>'get_types','help'=>'<分享拼满奖>:当自己的奖励满多少时，才能领取奖励！'),
			array('type'=>'unitstring','unit'=>'个','title'=>'满多少提取？','name'=>'pluginset.red_man','help'=>'<分享拼满奖>类型下才有效，当自己的奖励满多少时，才能领取奖励！'),	
			array(
							'type'=>'array',
							'name'=>'red_shareset',
							'title'=>'拉新奖励阶梯',
							'add_tip'=>'添加一个阶梯',
								'list'=>array(
									array('type'=>'string','name'=>'num','title'=>'多少人内'),
									array('type'=>'string','name'=>'red_max','title'=>'拉新奖励上限/人'),
									array('type'=>'string','name'=>'red_min','title'=>'拉新奖励下限/人'),
								),
								'details'=>array(
									array('type'=>'string','name'=>'num','title'=>'多少人内'),
									array('type'=>'string','name'=>'red_max','title'=>'拉新奖励上限/人'),
									array('type'=>'string','name'=>'red_min','title'=>'拉新奖励下限/人'),
								)
			),
			array('name'=>'type','title'=>'参与条件','type'=>'radio','options'=>'types'),
			array('name'=>'goodsid','title'=>'选择商品','type'=>'choose','help'=>'','action'=>'goods.get_goods/','show'=>array('type'=>2)),
			array('name'=>'price','title'=>'最低订单金额','type'=>'string','help'=>'','show'=>array('type'=>1)),
			array('name'=>'time','title'=>'参与时机','type'=>'radio','options'=>'times','show'=>array('type !='=>3),'help'=>'弹窗，提醒参与活动的时机！'),
			array('name'=>'pluginset.total_fee','title'=>'活动总额限制','type'=>'string'),
			array('name'=>'pluginset.total','title'=>'活动总人次限制','type'=>'string'),
			array('name'=>'pluginset.red_min','title'=>'活动介绍','type'=>'text'),
			array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称'),
			array('type'=>'image','name'=>'sendimage','title'=>'推送封面'),
			array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称'),	
			array('name'=>'status','title'=>'是否开启','type'=>'switch'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	model('core/php_ctrl')->del(array(
		'tablename'=>$this->tablename('game'),
	));
}
